package com.by.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.by.bean.ResultBean;

import com.by.entity.Order;
import com.by.query.LiOrderQuery;
import com.by.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "订单管理")
@RequestMapping("/api")
public class OrderController extends BaseController {

    @Autowired
    OrderService orderService;

    /**
     * 查询订单列表
     * @param
     * @return
     */
    @PutMapping("/order/list")
    @ApiOperation("查询订单列表")
    public ResultBean query(@RequestBody LiOrderQuery liOrderQuery) {
        //查询条件
        QueryWrapper<Order> objectQueryWrapper = new QueryWrapper<>();
        // 定义查询条件映射
        Map<String, String> conditionMap = new HashMap<>();
        conditionMap.put("sn", liOrderQuery.getSn());
        conditionMap.put("member_name", liOrderQuery.getMemberName());
        conditionMap.put("order_type", liOrderQuery.getOrderType());

        // 遍历映射添加条件
        for (Map.Entry<String, String> entry : conditionMap.entrySet()) {
            String field = entry.getKey();
            String value = entry.getValue();
            if (value != null && !value.isEmpty()) {
                objectQueryWrapper.eq(field, value);
            }
        }
        List<Map<String, Object>> maps = orderService.getBaseMapper().selectMaps(objectQueryWrapper);
        return success("ok", maps);
    }

    /**
     * 根据id查询订单详情
     * @param id
     * @return
     */
    @GetMapping("/order")
    public ResultBean queryById(int id){
        Order order = orderService.getById(id);
        return success("订单详情",order);
    }

    /**
     * 保存订单
     * @param order
     * @return
     */
    @PostMapping("/order")
    @ApiOperation("生成订单")
    public ResultBean save(@RequestBody Order order){
        Boolean save = false;
        if (!ObjectUtil.isEmpty(order))
            save = orderService.save(order);

       return save?success("保存订单成功"):fail("保存订单失败");
    }

    @DeleteMapping("/order")
    @ApiOperation("删除订单")
    public ResultBean delete(int id){
        boolean b = orderService.removeById(id);
        return b?success("删除订单成功"):fail("删除订单失败");
    }

//
//    /**
//     *
//     * @param id
//     * @return
//     */
//    public ResultBean changePrice(int id){
//
//    }

}
